﻿using LxCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace LxCore
{
    public class ZoneCond : JsObject, IIdentity
    {
        public ZoneCond(FvZone zone)
        {
            this.Zone = zone;
            InnerCond = new InnerCond();
            foreach (var b in zone.Bounds)
            {
                BoundConds.Add(new BoundCond(b));
            }
        }
        public FvZone Zone;

        private InnerCond _InnCond;
        public InnerCond InnerCond
        {
            get { return _InnCond; }
            set { _InnCond = value; RaisePropertyChanged(nameof(InnerCond)); }
        }

        private BoundCondList _BoundConds = new();
        public BoundCondList BoundConds
        {
            get { return _BoundConds; }
            set { _BoundConds = value; RaisePropertyChanged(nameof(BoundConds)); }
        }

        private PhysicalModel _PhysicalModel;
        public PhysicalModel PhysicalModel
        {
            get => _PhysicalModel;
            set
            {
                _PhysicalModel = value;
                RaisePropertyChanged(nameof(PhysicalModel));
                foreach (var t in BoundConds)
                {
                    t.CheckSetBound();
                }
            }
        }
        public string Name
        {
            get => Zone.Name;
            set
            {
                Zone.Name = value;
                RaisePropertyChanged(nameof(Name));
            }
        }
        public int ID
        {
            get => Zone.ID;
            set
            {
                Zone.ID = value;
                RaisePropertyChanged(nameof(ID));
            }
        }

        public FieldBase<double> EnergySource_b
        {
            get => _EnergySource_b;
            set
            {
                _EnergySource_b = value;
                RaisePropertyChanged(nameof(EnergySource_b));
            }
        }
        private FieldBase<double> _EnergySource_b = new ConstField.Double(0);
        public FieldBase<double> EnergySource_a
        {
            get => _EnergySource_a;
            set
            {
                _EnergySource_a = value;
                RaisePropertyChanged(nameof(EnergySource_a));
            }
        }
        private FieldBase<double> _EnergySource_a = new ConstField.Double(0);

        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(PhysicalModel): PhysicalModel = LxSim.Sim.Physics.PhysicalModels[jsReader.ReadString()]; break;
                case nameof(Name): Name = jsReader.ReadString(); break;
                case nameof(EnergySource_a): jsReader.ReadObject(EnergySource_a, e => EnergySource_a = e); break;
                case nameof(EnergySource_b): jsReader.ReadObject(EnergySource_b, e => EnergySource_b = e); break;
                case nameof(BoundConds): jsReader.ReadList(BoundConds, null); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            if (PhysicalModel == null) return;

            jsWriter.WriteString(nameof(PhysicalModel), PhysicalModel.Name);
            if (PhysicalModel.IsEnergyEnabled)
            {
                jsWriter.WriteObject(nameof(EnergySource_a), EnergySource_a, true);
                jsWriter.WriteObject(nameof(EnergySource_b), EnergySource_b, true);
            }
            jsWriter.WriteList(nameof(BoundConds), BoundConds);
        }
        public class BoundCondList : JsList<BoundCond>
        {

        }
    }
}
//foreach (var t in InZone.Bounds)
//{
//    t.CFDSetting.CheckSetBound();
//}